#include <GL/glew.h>
#include "CityViewer.hpp"

void CityViewer::init()
{
  //WARNING nothing before gl initialisation !
  //glew init for occlusion
  printf("CityBuilder v1.0 \"Pissed Mushroom\" release\n"
	 "Developed under terms of GNU General Public License v3\n"
	 "By Flavien Busseuil, Laurent Muthelet, Rodrigo Castro and Florian Deconinck\n"
	 "http://code.google.com/p/city-builder-mif29/ for more infos\n"
	 );
  glewInit();
  // back face culling
  glCullFace(GL_BACK);
  glEnable(GL_CULL_FACE);
  // set up for transparency
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  // size of the scene, you can change for a bigger one
  sceneSize = 500;
  setSceneRadius(sceneSize);
  showEntireScene();
  wiremode = false;
  bfc = true; // back-face culling
  grid = false;
  customMouseBehavior=false;

  look = qglviewer::Vec();
  
  // Init City
  archi = new Architect();
  city = new Map(archi, 1);
  city->setLOD(*camera());
}

void CityViewer::draw()
{
  float lightPos[4] = {0, 0, 100, 1};
  // Directionnal light
  glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
  
  if(grid)
    // draw a grid (unit = 1 meter)
    drawGrid(sceneSize, sceneSize*2);

  city->draw();
}

QString CityViewer::helpString() const
{
  QString text("<h2>CityBuilder v1.0 \" Pissed Mushroom\" release</h2>"
	       "Rand, Generate and build a huge city !");
  return text;
}

void CityViewer::keyPressEvent(QKeyEvent *e)
{
  switch(e->key())
  {
    case Qt::Key_G:
      grid = !grid;
      break;
    case Qt::Key_W:
      wiremode = !wiremode;
      if (wiremode)
        glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
      else
        glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
      break;
    case Qt::Key_B:
      bfc = !bfc;
      if(bfc)
        glEnable(GL_CULL_FACE);
      else
        glDisable(GL_CULL_FACE);
      break;
    //camera moving
    case Qt::Key_R://reset position of the cam
      look.setValue(0,1,0);
      camera()->setViewDirection(look);
      look.setValue(0,0,3);
      camera()->setPosition(look);
      break;
    case Qt::Key_Z:
      look[1]+=0.5;
      camera()->setPosition(look);
      break;
    case Qt::Key_S:
      look[1]-=0.5;
      camera()->setPosition(look);
      break;
    case Qt::Key_Q:
      look[0]-=0.5;
      camera()->setPosition(look);
      break;
    case Qt::Key_D:
      look[0]+=0.5;
      camera()->setPosition(look);
      break;
    /*
    case Qt::Key_A:
      customMouseBehavior=!customMouseBehavior;
    */
    default:
    /* Call the default handler */
    QGLViewer::keyPressEvent(e);
  }
  /* Update the screen */
  updateGL();
}
/*
void CityViewer::mousePressEvent(QMouseEvent* e)
{
   if ((e->state() & Qt::KeyButtonMask) == Qt::Key_A)
   customMouseBehavior = true;
   else
   QGLViewer::mousePressEvent(e);
}

void CityViewer::mouseMoveEvent(QMouseEvent *e)
{
  if (customMouseBehavior)
  {
    printf("here\n");
    look[0] = e->x();
    look[1] = e->y();
    camera()->setViewDirection(look);
  }
  else
    QGLViewer::mouseMoveEvent(e);
}

void CityViewer::mouseReleaseEvent(QMouseEvent* e)
{
  if (customMouseBehavior)
    customMouseBehavior = false;
  else
    QGLViewer::mouseReleaseEvent(e);
}
*/